package edu.gatech.teamrocket;

import edu.gatech.teamrocket.models.Auth;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.EditText;

/**
 * The Login form which utilizes login.xml
 * @author Patrick
 *
 */
public class LoginActivity extends Activity implements OnClickListener {
	DewApplication app;
	Auth auth;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (DewApplication) getApplicationContext();
		auth = app.getAuth();
		// setting default screen to login.xml
		setContentView(R.layout.login);
		((Button) findViewById(R.id.loginButton)).setOnClickListener(this);
		((Button) findViewById(R.id.registerButton)).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
	    	case R.id.loginButton:
		    	String username = ((EditText) findViewById(R.id.username)).getText().toString();
		    	String password = ((EditText) findViewById(R.id.password)).getText().toString();
		    	if (!auth.login(username, password)) { // is this a valid login?
		    		Toast.makeText(this, "Invalid username or password.", Toast.LENGTH_LONG).show();
		    	} else {
		    		Toast.makeText(this, "Success!", Toast.LENGTH_LONG).show();
		    		Intent i = new Intent(getApplicationContext(), ListActivity.class);
		    		i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // remove this activity from history
		    		startActivity(i);
		    		finish();
		    	}
		    	break;
		    case R.id.registerButton:
	        	Intent i = new Intent(getApplicationContext(), RegisterActivity.class); // get the activity for the registration form
	        	startActivity(i);
		    	break;
		}
	}
}
